package com.pig4cloud.pig.mt.manage;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.mt.entity.WxMsgTemplate;
import com.pig4cloud.pig.mt.form.TemplateMsgBatchForm;
import com.pig4cloud.pig.mt.service.WxMsgTemplateService;
import com.pig4cloud.pig.mt.service.WxTemplateMsgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * 消息模板
 *
 * @author niefy
 * @email niefy@qq.com
 * @date 2019-11-12 18:30:15
 */
@RestController
@RequestMapping("/manage/msgTemplate")
@Api(value = "/manage/msgTemplate", tags = "消息模板管理")
public class MsgTemplateManageController {
    @Autowired
    private WxMsgTemplateService msgTemplateService;
    @Autowired
    private WxTemplateMsgService templateMsgService;
    @Autowired
    private WxMpService wxMpService;

    /**
     * 列表
     */
	@ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/list")
	@PreAuthorize("@pms.hasPermission('mt_wxmsgtemplate_get')" )
    public R list(@CookieValue String appid, @RequestParam Map<String, Object> params) {
        params.put("appid", appid);
        IPage page = msgTemplateService.queryPage(params);

        return R.ok(page);
    }


    /**
     * 信息
     */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/info/{id}")
	@PreAuthorize("@pms.hasPermission('mt_wxmsgtemplate_get')" )
    public R info(@PathVariable("id") Long id) {
        WxMsgTemplate msgTemplate = msgTemplateService.getById(id);

        return R.ok(msgTemplate);
    }

    /**
     * 信息
     */
	@ApiOperation(value = "通过 name 查询", notes = "通过 name 查询")
    @GetMapping("/getByName")
	@PreAuthorize("@pms.hasPermission('mt_wxmsgtemplate_get')" )
    public R getByName(String name) {
        WxMsgTemplate msgTemplate = msgTemplateService.selectByName(name);

        return R.ok(msgTemplate);
    }

    /**
     * 保存
     */
	@ApiOperation(value = "新增消息模板", notes = "新增消息模板")
	@SysLog("新增消息模板")
    @PostMapping("/save")
	@PreAuthorize("@pms.hasPermission('mt_wxmsgtemplate_add')" )
    public R save(@RequestBody WxMsgTemplate msgTemplate) {
        msgTemplateService.save(msgTemplate);

        return R.ok();
    }

    /**
     * 修改
     */
	@ApiOperation(value = "修改消息模板", notes = "修改消息模板")
	@SysLog("修改消息模板")
    @PutMapping("/update")
	@PreAuthorize("@pms.hasPermission('mt_wxmsgtemplate_edit')" )
    public R update(@RequestBody WxMsgTemplate msgTemplate) {
        msgTemplateService.updateById(msgTemplate);

        return R.ok();
    }

    /**
     * 删除
     */
	@ApiOperation(value = "通过id删除消息模板", notes = "通过id删除消息模板")
	@SysLog("通过id删除消息模板")
    @DeleteMapping("/delete")
	@PreAuthorize("@pms.hasPermission('mt_wxmsgtemplate_del')" )
    public R delete(@RequestBody String[] ids) {
        msgTemplateService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

	/**
	 * 批量向用户发送模板消息
	 * 通过用户筛选条件（一般使用标签筛选），将消息发送给数据库中所有符合筛选条件的用户
	 */
	@ApiOperation(value = "批量向用户发送模板消息", notes = "批量向用户发送模板消息")
	@SysLog("批量向用户发送模板消息")
	@PostMapping("/sendMsgBatch")
	@PreAuthorize("@pms.hasPermission('mt_wxmsgtemplate_add')" )
	public R sendMsgBatch(@CookieValue String appid, @RequestBody TemplateMsgBatchForm form) {
		this.wxMpService.switchoverTo(appid);
		templateMsgService.sendMsgBatch(form, appid);
		return R.ok();
	}

    /**
     * 同步公众号模板
     */
	@ApiOperation(value = "同步公众号模板", notes = "同步公众号模板")
	@SysLog("同步公众号模板")
    @PostMapping("/syncWxTemplate")
	@PreAuthorize("@pms.hasPermission('mt_wxmsgtemplate_add')" )
    public R syncWxTemplate(@CookieValue String appid) throws WxErrorException {
        this.wxMpService.switchoverTo(appid);
        msgTemplateService.syncWxTemplate(appid);
        return R.ok();
    }


}
